package com.dries.portal.module.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dries.common.api.CommonResult;
import com.dries.portal.module.portal.entity.Discuss;
import com.dries.portal.module.portal.entity.DiscussDetail;
import com.dries.portal.module.portal.entity.DiscussReply;
import com.dries.portal.module.portal.mapper.DiscussDetailMapper;
import com.dries.portal.module.portal.service.IDiscussDetailService;
import com.dries.portal.module.portal.service.IDiscussReplyService;
import com.dries.portal.module.portal.service.IDiscussService;
import com.dries.portal.module.portal.vo.DiscussDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class DiscussDetailServiceImpl extends ServiceImpl<DiscussDetailMapper, DiscussDetail> implements IDiscussDetailService {

    @Resource
    private IDiscussService discussService;
    @Resource
    private IDiscussReplyService discussReplyService;

    /**
     * 获取用户评价详情
     * @param id
     */
    @Override
    public CommonResult getDiscussDetailById(Integer id) {
        DiscussDetailVo detailVo = new DiscussDetailVo();

        // 1.根据评价ID 查询评价详情
        DiscussDetail discussDetail = baseMapper.selectOne(
                new QueryWrapper<DiscussDetail>().lambda()
                        .eq(DiscussDetail::getDiscussId, id)
        );
        BeanUtils.copyProperties(discussDetail, detailVo);
        detailVo.setDiscussImgs(Arrays.asList(discussDetail.getDiscussImgs().split(",")));

        // 2.根据查询到的评价详情 查询评价
        Discuss discuss = discussService.getById(id);
        BeanUtils.copyProperties(discuss, detailVo);

        // 3.根据查询到的评价详情，查询用户回复的内容
        // TODO
        List<DiscussReply> replies = discussReplyService.list(
                new QueryWrapper<DiscussReply>().lambda()
                        .eq(DiscussReply::getDiscussDetailId, discussDetail.getId())
                .orderByDesc(DiscussReply::getCreateTime)
        );

        List<DiscussReply> replyList = replies.stream().filter(e -> e.getParentId() == 0)
                .peek(e -> e.setReplies(getReplies(e, replies))).collect(Collectors.toList());

        detailVo.setReplies(replyList);

        // 更新用户评价浏览量
        Integer pageView = subPageView(discussDetail.getId(), discussDetail.getDiscussPageView() == null ? 0 : discussDetail.getDiscussPageView());
        detailVo.setDiscussPageView(pageView);
        return CommonResult.success(detailVo);

    }

    /**
     * 递归查找所有评价的子评价
     */
    public List<DiscussReply> getReplies(DiscussReply reply,List<DiscussReply> replies){

        return replies.stream().filter(e -> Objects.equals(e.getParentId(), reply.getId()))
                .peek(e -> e.setReplies(getReplies(e, replies))).collect(Collectors.toList());
    }

    /**
     * 用户访问+1
     *
     * @param id
     */
    @Override
    public synchronized Integer subPageView(Integer id, Integer pageView) {
        DiscussDetail discussDetail = new DiscussDetail();
        discussDetail.setId(id);
        discussDetail.setDiscussPageView(++pageView);
        updateById(discussDetail);

        return pageView;
    }
}